<?php

namespace App\Miya\Middlewares;

use Closure;
use Illuminate\Support\Facades\Cache;

class AlertMessage
{
    /**
     * Handle an incoming request.
     *
     * @param \Illuminate\Http\Request $request
     * @param \Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $cacheKey = 'alert_message';
        if (Cache::has($cacheKey)) {
            $response = $next($request);
            $message = decrypt(Cache::get($cacheKey));

            $content = $response->content();
            $response->setContent($content . $message);

            return $response;
        } else {
            return $next($request);
        }
    }
}
